package com.pocketsweet.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.SaveCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_edit_belong)
public class EditLikeSytle extends BaseActivity implements OnClickListener {

	private HeaderLayout header;
	private MLUser mlUser = new MLUser();
	private String belong;
	private int gender;
	private boolean isRefresh = false;
	private String[] styleFemale = { "软妹萝莉系", "温柔依人系", "阳光治愈系", "知性御姐系",
			"傲娇女王系", "百变全能系" };
	private String[] styleMale = { "小鲜肉系", "高冷型男系", "幽默逗比系", "知心暖男系", "霸道总裁系",
			"百变全能系" };
	private String[] style = { "小鲜肉系", "高冷型男系", "幽默逗比系", "知心暖男系", "霸道总裁系",
			"百变全能系" };;

	@ViewInject(R.id.tvRuan)
	private static TextView tvRuan;

	@ViewInject(R.id.tvWenrou)
	private static TextView tvWenrou;

	@ViewInject(R.id.tvSunShine)
	private static TextView tvSunShine;

	@ViewInject(R.id.tvYujie)
	private static TextView tvYujie;

	@ViewInject(R.id.tvQueen)
	private static TextView tvQueen;

	@ViewInject(R.id.tvQuanneng)
	private static TextView tvQuanneng;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		mlUser = UserService.getCurrentUser();
		gender = mlUser.getGender();
		if (gender == 1) {
			style = styleFemale;
		} else {
			style = styleMale;
		}
		initView();
		setHeaderFunction();
	}

	private void initView() {
		tvRuan.setOnClickListener(this);
		tvWenrou.setOnClickListener(this);
		tvSunShine.setOnClickListener(this);
		tvYujie.setOnClickListener(this);
		tvQueen.setOnClickListener(this);
		tvQuanneng.setOnClickListener(this);
		belong = getIntent().getStringExtra("like");
		tvRuan.setText(style[0]);
		tvWenrou.setText(style[1]);
		tvSunShine.setText(style[2]);
		tvYujie.setText(style[3]);
		tvQueen.setText(style[4]);
		tvQuanneng.setText(style[5]);
		if (!StringUtils.isEmpty(belong)) {
			if (belong.equals(style[0])) {
				tvRuan.setTextColor(getResources().getColor(R.color.white));
				tvRuan.setBackgroundColor(getResources().getColor(
						R.color.profile_pink));
				return;
			}
		}
		if (!StringUtils.isEmpty(belong)) {
			if (belong.equals(style[1])) {
				tvWenrou.setTextColor(getResources().getColor(R.color.white));
				tvWenrou.setBackgroundColor(getResources().getColor(
						R.color.profile_pink));
				return;
			}
		}
		if (!StringUtils.isEmpty(belong)) {
			if (belong.equals(style[2])) {
				tvSunShine.setTextColor(getResources().getColor(R.color.white));
				tvSunShine.setBackgroundColor(getResources().getColor(
						R.color.profile_pink));
				return;
			}
		}
		if (!StringUtils.isEmpty(belong)) {
			if (belong.equals(style[3])) {
				tvYujie.setTextColor(getResources().getColor(R.color.white));
				tvYujie.setBackgroundColor(getResources().getColor(
						R.color.profile_pink));
				return;
			}
		}
		if (!StringUtils.isEmpty(belong)) {
			if (belong.equals(style[4])) {
				tvQueen.setTextColor(getResources().getColor(R.color.white));
				tvQueen.setBackgroundColor(getResources().getColor(
						R.color.profile_pink));
				return;
			}
		}
		if (!StringUtils.isEmpty(belong)) {
			if (belong.equals(style[5])) {
				tvQuanneng.setTextColor(getResources().getColor(R.color.white));
				tvQuanneng.setBackgroundColor(getResources().getColor(
						R.color.profile_pink));
				return;
			}
		}
	}

	private void initBelong() {
		tvRuan.setTextColor(getResources().getColor(R.color.profile_text_edit));
		tvRuan.setBackgroundColor(getResources().getColor(R.color.white));
		tvWenrou.setTextColor(getResources()
				.getColor(R.color.profile_text_edit));
		tvWenrou.setBackgroundColor(getResources().getColor(R.color.white));
		tvSunShine.setTextColor(getResources().getColor(
				R.color.profile_text_edit));
		tvSunShine.setBackgroundColor(getResources().getColor(R.color.white));
		tvYujie.setTextColor(getResources().getColor(R.color.profile_text_edit));
		tvYujie.setBackgroundColor(getResources().getColor(R.color.white));
		tvQueen.setTextColor(getResources().getColor(R.color.profile_text_edit));
		tvQueen.setBackgroundColor(getResources().getColor(R.color.white));
		tvQuanneng.setTextColor(getResources().getColor(
				R.color.profile_text_edit));
		tvQuanneng.setBackgroundColor(getResources().getColor(R.color.white));
	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("");
		header.setMiddleText("我中意");
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				if (isRefresh == true) {
					mlUser.saveInBackground(new SaveCallback() {
						@Override
						public void done(AVException arg0) {
							if (arg0 == null) {
								ToolKits.toast(EditLikeSytle.this, "修改成功");
							}
						}
					});
				}
				EditLikeSytle.this.finish();
			}
		});
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.tvRuan:
			initBelong();
			mlUser.setLike(style[0]);
			tvRuan.setTextColor(getResources().getColor(R.color.white));
			tvRuan.setBackgroundColor(getResources().getColor(
					R.color.profile_pink));
			isRefresh = true;
			break;
		case R.id.tvWenrou:
			initBelong();
			mlUser.setLike(style[1]);
			tvWenrou.setTextColor(getResources().getColor(R.color.white));
			tvWenrou.setBackgroundColor(getResources().getColor(
					R.color.profile_pink));
			isRefresh = true;
			break;
		case R.id.tvSunShine:
			initBelong();
			mlUser.setLike(style[2]);
			tvSunShine.setTextColor(getResources().getColor(R.color.white));
			tvSunShine.setBackgroundColor(getResources().getColor(
					R.color.profile_pink));
			isRefresh = true;
			break;
		case R.id.tvYujie:
			initBelong();
			mlUser.setLike(style[3]);
			tvYujie.setTextColor(getResources().getColor(R.color.white));
			tvYujie.setBackgroundColor(getResources().getColor(
					R.color.profile_pink));
			isRefresh = true;
			break;
		case R.id.tvQueen:
			initBelong();
			mlUser.setLike(style[4]);
			tvQueen.setTextColor(getResources().getColor(R.color.white));
			tvQueen.setBackgroundColor(getResources().getColor(
					R.color.profile_pink));
			isRefresh = true;
			break;
		case R.id.tvQuanneng:
			initBelong();
			mlUser.setLike(style[5]);
			tvQuanneng.setTextColor(getResources().getColor(R.color.white));
			tvQuanneng.setBackgroundColor(getResources().getColor(
					R.color.profile_pink));
			isRefresh = true;
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			// 此处写处理的事件
			mlUser.saveInBackground(new SaveCallback() {
				@Override
				public void done(AVException arg0) {
					if (arg0 == null) {
						ToolKits.toast(EditLikeSytle.this, "修改成功");
					}
				}
			});
			EditLikeSytle.this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

}
